<?php
/*
Plugin Name: 图集-附件分类
Plugin URI: http://blog.wpjam.com/project/wpjam-basic/
Description: 可以给附件设置分类。
Version: 1.0
*/
add_filter('wpjam_taxonomies','wpjam_taxonomies_add_media_collection');
function wpjam_taxonomies_add_media_collection($wpjam_taxonomies){
	$wpjam_taxonomies['collection'] = array(
		'object_type'   => array('attachment'),
		'args'			=> array(
			'label'					=> '图集', 
			'hierarchical'			=> false, 
			'show_ui'				=> true,
			'show_in_nav_menus'		=> false,
			'show_admin_column'		=> true,
			'query_var'				=> true, 
			'rewrite'				=> false,
			'update_count_callback'	=> 'wpjam_media_collection_update_count_callback'
		)
	);

	return $wpjam_taxonomies;
}

function wpjam_media_collection_update_count_callback($terms, $taxonomy){	// 附件则必须要用这个数量更新回调函数
	_update_generic_term_count( $terms, $taxonomy );
	clean_term_cache($terms, '', false);
}

add_action( 'restrict_manage_posts', 'wpjam_media_collection_restrict_manage_posts' );
function wpjam_media_collection_restrict_manage_posts() {
	global $pagenow;
	if ( 'upload.php' == $pagenow ) {

		$media_library_mode = get_user_option( 'media_library_mode' ) ? get_user_option( 'media_library_mode' ) : 'grid';
		if($media_library_mode == 'grid') return;

		$selected = isset($_GET['collection'])?$_GET['collection']:'';
		
		wp_dropdown_categories(array(
			'taxonomy'			=> 'collection',
			'name'				=> 'collection',
			'show_option_all'	=> '所有图集',
			'hierarchical'		=> 1,
			'show_count'		=> 1,
			'value_field'		=> 'slug',
			// 'hide_empty'		=> 0,
			'selected'			=> $selected,
		));
	}
}

add_filter('attachment_fields_to_edit', 'wpjam_media_collection_attachment_fields_to_edit',10,2);
function wpjam_media_collection_attachment_fields_to_edit($form_fields, $post){
	foreach ( get_attachment_taxonomies($post) as $taxonomy ) {
		$t = (array) get_taxonomy($taxonomy);
		if ( ! $t['public'] || ! $t['show_ui'] )
			continue;
		if ( empty($t['label']) )
			$t['label'] = $taxonomy;
		if ( empty($t['args']) )
			$t['args'] = array();

		$terms = get_object_term_cache($post->ID, $taxonomy);
		if ( false === $terms )
			$terms = wp_get_object_terms($post->ID, $taxonomy, $t['args']);

		$values = array();

		foreach ( $terms as $term )
			$values[] = $term->name;
		$t['value'] = join(', ', $values);

		// if ( $t['hierarchical'] ) {
		// 	$html = wp_terms_checklist( $post->ID, array( 'taxonomy' => $taxonomy, 'checked_ontop' => false, 'echo' => false ) );

		// 	$html = '<ul class="term-list">' . $html . '</ul>';

		// 	unset( $t['value'] );

		// 	$t['input'] = 'html';
		// 	$t['html'] = $html;
		// }

		$form_fields[$taxonomy] = $t;
	}

	return $form_fields;
}

add_action( 'wp_enqueue_media', 'wpjam_media_collection_enqueue_media',99);
function wpjam_media_collection_enqueue_media() {
	wp_enqueue_script( 'media-collection-filter', WPJAM_BASIC_PLUGIN_URL.'/template/static/media-collection-filter.js', array( 'media-editor', 'media-views' ) );
	
	wp_localize_script( 'media-collection-filter', 'MediaColletionFilterData', array(
		'terms'     => get_terms( 'collection', array( 'hide_empty' => false ) ),
	) );

	wp_add_inline_style( 'media-views', '.media-modal-content .media-frame select.attachment-filters {
		max-width: -webkit-calc(33% - 12px);
		max-width: calc(33% - 12px);
	}');
}